package cn.thoughtworks.school.repositories;


import cn.thoughtworks.school.entities.Serial;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SerialRepository extends JpaRepository<Serial,Long> {
  List<Serial> findByCreatorId(Long userId);

  List<Serial> findFirst8ByIsAllVisibleIsTrueAndIsReleaseIsTrueOrOrganizationIdAndIsReleaseIsTrueOrderByCreateTimeDesc(Long userCurrentOrganizationId);

  Page<Serial> findByIsAllVisibleIsTrueAndIsReleaseIsTrueOrOrganizationIdAndIsReleaseIsTrueOrderByCreateTimeDesc(Long userCurrentOrganizationId, Pageable pageable);

  @Query(value = "select s from Serial s where s.id in (select " +
    "s.serialId from SerialSubscription s where s.userId = ?1 )")
  List<Serial> getMySubscriptionSerials(Long id);

}
